package com.csu.tour.service.impl;

import com.csu.tour.entity.PageBean;
import com.csu.tour.entity.Complaint;
import com.csu.tour.mapper.ComplaintMapper;
import com.csu.tour.service.ComplaintService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    private ComplaintMapper complaintMapper;

    @Override
    public void evaluation(int complaintId, String evaluation) {
        complaintMapper.evaluation(complaintId,evaluation);
    }

    @Override
    public void statusApprove(int complaintId, Complaint.ComplaintStatusApprove statusApprove) {
        complaintMapper.statusApprove(complaintId,statusApprove);
        if(statusApprove == Complaint.ComplaintStatusApprove.processing) {
            complaintMapper.handlingAdd(complaintId);
        }
    }

    @Override
    public void complaintAdd(Complaint complaint) {
        complaint.setDate(LocalDateTime.now());
        complaint.setStatusApprove(Complaint.ComplaintStatusApprove.pending);
        complaintMapper.complaintAdd(complaint);
    }

    @Override
    public void process(int complaintId, String handlingContent, Complaint.ComplaintStatus status) {
        complaintMapper.process(complaintId,handlingContent,status);
    }

    @Override
    public void finish(int complaintId, Complaint.ComplaintStatusApprove statusApprove) {
        complaintMapper.finish(complaintId, statusApprove);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, Complaint.ComplaintStatus status,
                         Complaint.ComplaintStatusApprove statusApprove, LocalDate begin, LocalDate end) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);

        //2. 执行查询
        List<Complaint> complaintList = complaintMapper.list(status,statusApprove, begin, end);
        Page<Complaint> p = (Page<Complaint>) complaintList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
}
